package tt.dz.mvc.controller.base;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import tt.dz.entity.base.JsonCommon;
import tt.dz.entity.base.JsonForList;
import tt.dz.entity.station.OmsStation;
import tt.dz.entity.station.OmsStationCollect;
import tt.dz.entity.station.OmsStationExternal;
import tt.dz.service.base.StationCollectService;
import tt.dz.service.sys.StationExternalService;
import tt.dz.service.sys.StationService;
import tt.dz.util.ActionUtils;

/**
 * 
 * @author huanghao
 *
 */
@Controller
@RequestMapping("stationCollectApi")
public class StationCollectAction extends BaseAction
{
	@Autowired
	private StationCollectService stationCollectServiceImpl;
	@Autowired
	private StationService stationServiceImpl;
	@Autowired
	private StationExternalService stationExternalServiceImpl;
	
	@RequestMapping(value="add",method=RequestMethod.POST)
	@ResponseBody
	public JsonCommon<?> add(HttpServletRequest request, HttpServletResponse response)
	{
		return stationCollectServiceImpl.add(this.getObjFromRequest(request,true,false));
	}
	
	@RequestMapping(value="update/{id}",method=RequestMethod.POST)
	@ResponseBody
	public JsonCommon<?> updateOperMerchant(@PathVariable(value="id") String id,
			HttpServletRequest request, HttpServletResponse response)
	{
		OmsStationCollect obj = this.getObjFromRequest(request,false,false);
		obj.setStation_id(id);
		return stationCollectServiceImpl.update(obj);
	}
	
	@RequestMapping(value="delete/{id}",method=RequestMethod.POST)
	@ResponseBody
	public JsonCommon<?> deleteOperMerchant(@PathVariable(value="id") String id,
			HttpServletRequest request, HttpServletResponse response)
	{
		return stationCollectServiceImpl.delete(id);
	}
	
	@RequestMapping(value="findAllOrPage",method=RequestMethod.GET)
	@ResponseBody
	public JsonForList<?> findAllOrPage(HttpServletRequest request, HttpServletResponse response)
	{
		return stationCollectServiceImpl.findPageObjs(this.buildQueryParam(request, true), this.getObjFromRequest(request,false,true));
	}
	
	@RequestMapping(value="findObj/{id}",method=RequestMethod.GET)
	@ResponseBody
	public JsonCommon<?> findObj(@PathVariable(value="id") String id,
			HttpServletRequest request, HttpServletResponse response)
	{
		return stationCollectServiceImpl.findById(id);
	}
	
	@RequestMapping(value="reviewPass",method=RequestMethod.POST)
	@ResponseBody
	public JsonCommon<?> reviewPass(HttpServletRequest request, HttpServletResponse response)
	{
		String stationType = request.getParameter("stationType");
		String entityId = request.getParameter("entityId");
		String entityType = request.getParameter("entityType");
		String collectId = request.getParameter("stationId");
		String userId = this.getLoginUserId(request);
		String stationId = null;
		if ("1".equals(stationType))
		{
			stationId = stationServiceImpl.getNewStationId(getOmsStationFromRequest(request));
		}
		else
		{
			stationId = stationExternalServiceImpl.getNewStationId(getOmsStationExternalFromRequest(request));
		}
		return stationCollectServiceImpl.addStationCollect2Station(collectId, stationId, stationType, entityId, entityType, userId);
	}
	
	private OmsStationCollect getObjFromRequest(HttpServletRequest request,boolean isCreate,boolean isFind)
	{
		String loginUserId = this.getLoginUserId(request);
		return ActionUtils.getParamInstance(OmsStationCollect.class, ActionUtils.getParameterMap(request),loginUserId,isCreate,isFind);
	}
	
	private OmsStation getOmsStationFromRequest(HttpServletRequest request)
	{
		String loginUserId = this.getLoginUserId(request);
		return ActionUtils.getParamInstance(OmsStation.class, ActionUtils.getParameterMap(request),loginUserId,false,true);
	}
	
	private OmsStationExternal getOmsStationExternalFromRequest(HttpServletRequest request)
	{
		String loginUserId = this.getLoginUserId(request);
		return ActionUtils.getParamInstance(OmsStationExternal.class, ActionUtils.getParameterMap(request),loginUserId,false,true);
	}
	
}
